package book_management.book;

import java.util.ArrayList;
import java.util.List;

public class BookList {
    /**
     * 书架
     * @author 胡伟龙
     * @date  2022年1月12日19:12:53
     */
    //books存放书架中的书，每个Book是一个实体
    private static List<Book> books = new ArrayList<>();
    //booksName存放了所有的书本名称 - 查询书籍等都是通过书名来操作，用来找书籍
    private static List<String> booksName = new ArrayList<>();
    //初始化书籍
    //使用static代码块初始化static变量
    static {
        books.add(new Book("三国演义","罗贯中",66.6,"小说"));
        books.add(new Book("红楼梦","曹雪芹",88.8,"小说"));
        books.add(new Book("水浒传","施耐庵",55.6,"小说"));
        books.add(new Book("西游记","吴承恩",88.6,"小说"));
        booksName.add("三国演义");
        booksName.add("红楼梦");
        booksName.add("水浒传");
        booksName.add("西游记");
    }
    public void displayBooks(){
        //遍历书籍
        for (Book book: books
             ) {
            System.out.println(book);
        }
    }
    //书本是否存在
    public boolean contains(String booksName){
        return BookList.booksName.contains(booksName);
    }
    //添加书本
    public void add(Book book){
        books.add(book);
        booksName.add(book.getBookName());
    }
    //删除书本
    public void del(Book book){
        books.remove(book);
        booksName.remove(book.getBookName());
    }
    //查询书本
    public Book find(String bookName){
        for (Book book: books
             ) {
            if (book.getBookName().equals(bookName)){
                return book;
            }
        }
        return null;
    }
    //更改书本
    public void upDate(Book book,double price,String type){
        book.setPrice(price);
        book.setType(type);
    }
    //借书
    public void borrow(Book book){
        book.setBorrowed(true);
    }
    //还书
    public void ret(Book book){
        book.setBorrowed(false);
    }
}
